package edu.hziee.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.hziee.dao.TeacherDao;
import edu.hziee.models.Student;
import edu.hziee.models.Teacher;



@Controller
@RequestMapping("/teacher")
public class TeacherController {

	@Autowired
	private TeacherDao teacherdao;
	
	@RequestMapping(value="/tlist",method=RequestMethod.GET)
	public ModelAndView getAll(){
		ModelAndView mav=new ModelAndView("teacher/teacherList");
		List<Teacher> teachers = teacherdao.findAll();
		mav.addObject("teachers",teachers);
		return mav;

	}
	
	@RequestMapping(value="/tadd",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("teacher/addteacher");
		return mav;
		
	}
	@RequestMapping(value="/tadd",method=RequestMethod.POST)
	public ModelAndView add(
			@RequestParam int tid,
			@RequestParam String tname,
			@RequestParam String tcourse,
			@RequestParam String tphone
			){
		Teacher teacher = new Teacher();
		teacher.setTid(tid);
		teacher.setTname(tname);
		teacher.setTcourse(tcourse);
		teacher.setTphone(tphone);
		teacherdao.addTeacher(teacher);
		ModelAndView mav=new ModelAndView("redirect:tlist");
		return mav;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam int tid){
		Teacher teacher = teacherdao.findById(tid);
		ModelAndView mav=new ModelAndView("teacher/editteacher");
		mav.addObject("teacher",teacher);
		return mav;
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public ModelAndView edit(
			@RequestParam int tid,
			@RequestParam String tname,
			@RequestParam String tcourse,
			@RequestParam String tphone
			){
		Teacher teacher = new Teacher();
		teacher.setTid(tid);
		teacher.setTname(tname);
		teacher.setTcourse(tcourse);
		teacher.setTphone(tphone);
		
		teacherdao.modify(teacher);
		ModelAndView mav=new ModelAndView("redirect:tlist");
		return mav;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(@RequestParam int id){
		teacherdao.delete(id);
		ModelAndView mav=new ModelAndView("redirect:tlist");
		return mav;
	}
}
